package com.cskaoyan.service;

import com.cskaoyan.bean.MarketSystem;
import com.cskaoyan.bean.MarketSystemExample;
import com.cskaoyan.mapper.MarketSystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author Austin Hsu
 * @Date 2022/9/7 15:32
 */

@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    MarketSystemMapper marketSystemMapper;

    /**
     * 接口68.1
     * 1.查询得到List<MarketSystem> list
     * 查询语句：select * from market_system where id in (3,7,9,13,14,16);
     * 2.ConfigMall congfigmall接收 遍历list
     * 判断语句：if key_name=xx 就configmall.set(xx)=list元素.get(key_value)
     *
     * @return
     */
    @Override
    public List<MarketSystem> mall() {
        MarketSystemExample example = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = example.createCriteria();

        Integer[] id = {3, 7, 9, 13, 14, 16};        //应该是用key_name做模糊定位  where key_name=%market_mall%
        List<Integer> idList = Arrays.asList(id);
        criteria.andIdIn(idList);

        List<MarketSystem> list = marketSystemMapper.selectByExample(example);

        return list;
    }


    /**
     * 接口68.2
     * 修改语句：updateSeletive set key_value #{map.value} -- where key_name=#{map.key} 循环
     *
     * @param map
     */
    @Override
    public void mall2(Map<String, String> map) {
        for (Map.Entry<String, String> entry : map.entrySet()) {    //遍历map条目集
            MarketSystemExample example = new MarketSystemExample();           //查询条件
            MarketSystemExample.Criteria criteria = example.createCriteria();  //example的内部类

            criteria.andKeyNameEqualTo(entry.getKey());    //得到请求的map的key
            //-------------------以上是where条件，以下是set参数----------------
            MarketSystem marketSystem = new MarketSystem();
            marketSystem.setKeyValue(entry.getValue());             //sql语句中set后面的语句
            //---------------------最后拼接而成的sql语句--------------------------
            marketSystemMapper.updateByExampleSelective(marketSystem, example);     //修改
        }
    }

    @Override
    public List<MarketSystem> express() {
        MarketSystemExample example = new MarketSystemExample();
//        MarketSystemExample.Criteria criteria = example.createCriteria();  全是or的时候可以不用内部类

        example.or().andKeyNameEqualTo("market_express_freight_min");
        example.or().andKeyNameEqualTo("market_express_freight_value");


        List<MarketSystem> list = marketSystemMapper.selectByExample(example);

        return list;
    }

    @Override
    public void express2(Map<String, String> map) {
        for (Map.Entry<String, String> entry : map.entrySet()) {    //遍历map条目集
            MarketSystemExample example = new MarketSystemExample();           //查询条件
            MarketSystemExample.Criteria criteria = example.createCriteria();  //example的内部类

            criteria.andKeyNameEqualTo(entry.getKey());    //得到请求的map的key
            //-------------------以上是where条件，以下是set参数----------------
            MarketSystem marketSystem = new MarketSystem();
            marketSystem.setKeyValue(entry.getValue());             //sql语句中set后面的语句
            //---------------------最后拼接而成的sql语句--------------------------
            marketSystemMapper.updateByExampleSelective(marketSystem, example);     //修改
        }
    }

    @Override
    public List<MarketSystem> order() {
        MarketSystemExample example = new MarketSystemExample();

        example.or().andKeyNameEqualTo("market_order_unconfirm");
        example.or().andKeyNameEqualTo("market_order_unpaid");
        example.or().andKeyNameEqualTo("market_order_comment");


        List<MarketSystem> list = marketSystemMapper.selectByExample(example);

        return list;
    }

    @Override
    public void order2(Map<String, String> map) {
        for (Map.Entry<String, String> entry : map.entrySet()) {    //遍历map条目集
            MarketSystemExample example = new MarketSystemExample();           //查询条件
            MarketSystemExample.Criteria criteria = example.createCriteria();  //example的内部类

            criteria.andKeyNameEqualTo(entry.getKey());    //得到请求的map的key
            //-------------------以上是where条件，以下是set参数----------------
            MarketSystem marketSystem = new MarketSystem();
            marketSystem.setKeyValue(entry.getValue());             //sql语句中set后面的语句
            //---------------------最后拼接而成的sql语句--------------------------
            marketSystemMapper.updateByExampleSelective(marketSystem, example);     //修改
        }
    }

    @Override
    public List<MarketSystem> wx() {
        MarketSystemExample example = new MarketSystemExample();

        example.or().andKeyNameEqualTo("market_wx_index_new");
        example.or().andKeyNameEqualTo("market_wx_index_topic");
        example.or().andKeyNameEqualTo("market_wx_share");
        example.or().andKeyNameEqualTo("market_wx_index_brand");
        example.or().andKeyNameEqualTo("market_wx_catlog_goods");
        example.or().andKeyNameEqualTo("market_wx_catlog_list");
        example.or().andKeyNameEqualTo("market_wx_index_hot");


        List<MarketSystem> list = marketSystemMapper.selectByExample(example);

        return list;
    }

    @Override
    public void wx2(Map<String, String> map) {
        for (Map.Entry<String, String> entry : map.entrySet()) {    //遍历map条目集
            MarketSystemExample example = new MarketSystemExample();           //查询条件
            MarketSystemExample.Criteria criteria = example.createCriteria();  //example的内部类

            criteria.andKeyNameEqualTo(entry.getKey());    //得到请求的map的key
            //-------------------以上是where条件，以下是set参数----------------
            MarketSystem marketSystem = new MarketSystem();
            marketSystem.setKeyValue(entry.getValue());             //sql语句中set后面的语句
            //---------------------最后拼接而成的sql语句--------------------------
            marketSystemMapper.updateByExampleSelective(marketSystem, example);     //修改
        }
    }
}
